package com.iot.core.receiver.connect;

import com.alibaba.fastjson2.JSON;
import com.iot.core.base.IotMessage;
import com.iot.core.base.IotSubTopic;
import com.iot.core.payload.ConnectPayload;
import com.iot.core.receiver.BaseReceiver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @auther 王霄
 * @date 2022/7/28 10:48
 */
@Component
@Slf4j
public class ConnectReceiver implements BaseReceiver {

    @Resource
    private OnlineProcessor onlineProcessor;
    @Resource
    private OfflineProcessor offlineProcessor;

    @Override
    public IotSubTopic getSubTopic() {
        return IotSubTopic.CONNECTED;
    }

    @Override
    public void receive(IotMessage iotMessage) {
        log.info("接收上下线消息，主题: " + iotMessage.getTopic() + ",消息载荷: " + iotMessage.getPayload());
        ConnectPayload connectPayload = JSON.parseObject(iotMessage.getPayload(), ConnectPayload.class);

        switch (connectPayload.getStatus()) {
            case "online":
                onlineProcessor.handler(connectPayload);
                break;
            case "offline":
                offlineProcessor.handler(connectPayload);
                break;
            default:
                break;
        }
    }
}
